package org.example;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Hello world!
 */
public class App {
    public static void main(String[] args) {
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("my-persistence-unit");
        EntityManager em = emf.createEntityManager();
        nativeQuery(em,"SHOW TABLES");
        nativeQuery(em,"SHOW COLUMNS FROM EMPLOYEE");
        nativeQuery(em,"SHOW COLUMNS FROM EMPLOYEE_ADDRESSES");
        nativeQuery(em,"SHOW COLUMNS FROM EMPLOYEE_PHONENUMBERS");
        nativeQuery(em,"SHOW COLUMNS FROM EMPLOYEE_TASK");
        nativeQuery(em,"SHOW COLUMNS FROM TASK");
        persistEmployees(em);
        nativeQuery(em,"SELECT * FROM EMPLOYEE");
        nativeQuery(em,"SELECT * FROM EMPLOYEE_ADDRESSES");
        nativeQuery(em,"SELECT * FROM EMPLOYEE_PHONENUMBERS");
        nativeQuery(em,"SELECT * FROM EMPLOYEE_TASK");
        nativeQuery(em,"SELECT * FROM TASK");
        em.close();
    }
    private static void persistEmployees(EntityManager em){
        Task task1 = Task.create("Development");
        Task task2 = Task.create("Documentation");
        Task task3 = Task.create("Designing");
        Task task4 = Task.create("Refactoring");
        Task task5 = Task.create("Testing");
        Employee employee1 = Employee.create("Diana", Arrays.asList(task1, task2, task5),
                Arrays.asList(
                        Address.create("111 Round Drive", "Papineau", "Sundland"),
                        Address.create("2623 Flmwood Avenue", "Scottsdale", "Zwonga")),
                "111-111-111", "666-666-666", "222-222-222");
        Employee employee2 = Employee.create("Denise",Arrays.asList(task2,task3),
                Arrays.asList(
                        Address.create("23 Estate Avenue","Papineau","Ugrela"),
                        Address.create("365 Rose Route","Scottsdale","Mreyton")
                ),"444-444-444","333-333-333");
        Employee employee3 = Employee.create("Linda",Arrays.asList(task1,task5),Arrays.asList(
                Address.create("91 Vine Lane","Binesville","Oblijan")
        ),"555-555-555");
        task1.setSupervisor(employee2);
        task2.setSupervisor(employee1);
        task3.setSupervisor(employee3);
        task4.setSupervisor(employee1);
        task5.setSupervisor(employee3);
        em.getTransaction().begin();
        em.persist(employee1);
        em.persist(employee2);
        em.persist(employee3);
        em.getTransaction().commit();
    }
    private static void nativeQuery(EntityManager em,String s){
        System.err.println("-------------" + s + "-------------------");
        Query query = em.createNativeQuery(s);
        List list = query.getResultList();
        for (Object o : list) {
            if (o instanceof Object[]) {
                System.err.println(Arrays.toString((Object[])o));
            }else{
                System.err.println(o);
            }
        }
    }
}
